#!/usr/bin/env python
# coding: utf-8

from google.appengine.ext import db

import module.utility
import data.model

class UserManager:
    @staticmethod
    def get_user_entry(loginid):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        # TODO Need to fix
        return q.fetch(1)[0]
    
    @staticmethod
    def get_user_list(start=0, limit=10):
        q = db.GqlQuery("SELECT * FROM Users")
        return q.fetch(limit, start)

    @staticmethod
    def add_new_user(loginid, email, deposit, adminkey=None):
        # Check parameter
        if not module.utility.Utility.check_empty_parameter([loginid, email, deposit]):
            return -2   # Required parameter is missed
        
        if UserManager.is_exist_userid(loginid):
            return -1   # ID already exists
        
        if UserManager.is_exist_email(email):
            return -3   # Email already exists
        
        # Insert new user
        u = data.model.Users()
        u.loginid = loginid
        u.email = email
        u.deposit = deposit
        u.put()
        
        return 0        # Success
    
    @staticmethod
    def add_dlist(loginid, gp_key):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        l = u.deposit_gp
        l.append(gp_key)
        u.deposit_gp = l
        u.put()
        return 0
    
    @staticmethod
    def add_nlist(loginid, gp_key):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        l = u.nondeposit_gp
        l.append(gp_key)
        u.nondeposit_gp = l
        u.put()
        return 0
    
    @staticmethod
    def del_dlist(loginid, gp_key):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        l = u.deposit_gp
        l.remove(gp_key)
        u.deposit_gp = l
        u.put()
        return 0
    
    @staticmethod
    def del_nlist(loginid, gp_key):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        l = u.nondeposit_gp
        l.remove(gp_key)
        u.nondeposit_gp = l
        u.put()
        return 0
    
    @staticmethod
    def is_exist_email(email, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE email = :1", email)
        result = q.fetch(1)
        if not result:
            return False
        else:
            return True
    
    @staticmethod
    def is_exist_userid(loginid, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        result = q.fetch(1)
        if not result:
            return False
        else:
            return True
        
    @staticmethod
    def del_user(loginid, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        u = user[0]
        u.delete()
        return 0
    
    @staticmethod
    def modify_user_info(loginid, email, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        if UserManager.is_exist_email(email):
            return -3       # Email already exists
        
        u = user[0]
        u.email = email
        u.put()
        return 0
    
    @staticmethod
    def withdraw_deposit(loginid, deposit=1000, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        
        remain = u.deposit-deposit
        if remain < 0:
            return -8
        u.deposit = remain
        u.put()
        return 0
    
    @staticmethod
    def add_deposit(loginid, deposit=1000, adminkey=None):
        q = db.GqlQuery("SELECT * FROM Users WHERE loginid = :1", loginid)
        user = q.fetch(1)
        
        if not user:
            return -4       # User is not exist in the database
        
        u = user[0]
        u.deposit = u.deposit+deposit
        u.put()
        return 0